//
//  SXWYDetailArticleViewController.m
//  ShiXun
//
//  Created by lanou on 15/10/29.
//  Copyright © 2015年 张琪. All rights reserved.
//

#import "SXWYDetailArticleViewController.h"
#import "SXRequestManager.h"
#import "UIView+UIView.h"
#import "SXWYDetailHeaderView.h"
#import "UMSocial.h"
#import "SXWYRelativeCell.h"
#import "SXWYRelativeModel.h"
#import "SXWYRelativeHeadView.h"
@interface SXWYDetailArticleViewController ()<UIWebViewDelegate, UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong) UIWebView *webView;
@property(nonatomic, strong) SXWYDetailHeaderView *headerView;
@property(nonatomic, strong) UITableView *mainTableView;
@property(nonatomic, assign) CGFloat webViewHeight;
@property(nonatomic, strong) NSMutableArray *dataArray;
@property(nonatomic, strong) SXWYRelativeHeadView *relativeView;
@end

@implementation SXWYDetailArticleViewController
- (UIWebView *)webView
{
    if (!_webView) {
        _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64)];
        _webView.backgroundColor = [UIColor whiteColor];
        _webView.delegate = self;
    }
    return _webView;
}
- (UITableView *)mainTableView
{
    if (_mainTableView == nil) {
        _mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(10, self.webViewHeight, kScreenWidth - 20, self.dataArray.count * (20 * (kScreenWidth - 30) / 81 + 140 / 27) + 40) style:UITableViewStylePlain];
        _mainTableView.delegate = self;
        _mainTableView.dataSource = self;
        [_mainTableView registerClass:[SXWYRelativeCell class] forCellReuseIdentifier:@"SXWYRelativeCell"];
        _mainTableView.tableHeaderView = self.relativeView;
    }
    return _mainTableView;
}
- (SXWYRelativeHeadView *)relativeView
{
    if (_relativeView == nil) {
        _relativeView = [[SXWYRelativeHeadView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 35)];
    }
    return _relativeView;
}
// 设置新闻的标题
- (SXWYDetailHeaderView *)headerView
{
    if (_headerView == nil) {
        _headerView = [[SXWYDetailHeaderView alloc] initWithFrame:CGRectMake(0, -70, kScreenWidth, 120)];
    }
    return _headerView;
}
- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.hidden = NO;
    [self requestData];
    [self setNavigationBar];
    [self.view addSubview:self.webView];
    [self.webView.scrollView addSubview:self.headerView];
    // Do any additional setup after loading the view.
}
- (void)requestData
{
    NSString *URLStr = [kWYNewsDetailURLString stringByAppendingString:[NSString stringWithFormat:@"%@/full.html", _docidStr]];
    [SXRequestManager requestWithType:GET urlString:URLStr parDic:nil finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableLeaves |NSJSONReadingMutableContainers) error:nil];
        NSDictionary *artDic = dic[_docidStr];
        NSString *htmlString = artDic[@"body"];
        NSArray *imgArray = artDic[@"img"];
        htmlString = [self.view webViewHtmlString:htmlString imgArray:imgArray];
        [self.webView loadHTMLString:htmlString baseURL:nil];
        self.headerView.titleLabel.text = artDic[@"title"];
        self.headerView.fromLabel.text = [NSString stringWithFormat:@"%@  %@", [artDic[@"ptime"] substringWithRange:NSMakeRange(5, 11)], artDic[@"source"]];
        
        if ([artDic[@"relative_sys"] count] > 0) {
            for (NSDictionary *relativeDic in artDic[@"relative_sys"]) {
                SXWYRelativeModel *model = [[SXWYRelativeModel alloc] init];
                [model setValuesForKeysWithDictionary:relativeDic];
                [self.dataArray addObject:model];
            }
        }
        else{
            self.relativeView.titleLabel.text = @"";
        }
    } error:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}
// 设置返回和分享按钮
- (void)setNavigationBar
{
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"SX-yuedufanhui"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStylePlain target:self action:@selector(didClickLeft)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"SX-yuedushare"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStylePlain target:self action:@selector(didClickRight)];
}
- (void)didClickLeft
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didClickRight
{
    [UMSocialConfig setFinishToastIsHidden:NO position:(UMSocialiToastPositionCenter)];
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"5632c41ce0f55a17390010c1"
                                      shareText:[NSString stringWithFormat:@"//分享新闻 %@ %@", self.headerView.titleLabel.text, _shareStr]
                                     shareImage:[UIImage imageNamed:@"icon.png"]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToDouban,UMShareToRenren,UMShareToInstagram, UMShareToSms,nil]
                                       delegate:nil];

}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    self.webViewHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue];
     self.webView.scrollView.contentSize = CGSizeMake(kScreenWidth, 45 + _webViewHeight + self.dataArray.count * (20 * (kScreenWidth - 30) / 81 + 140 / 27));
    [self.webView.scrollView addSubview:self.mainTableView];
    [self.mainTableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SXWYRelativeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SXWYRelativeCell" forIndexPath:indexPath];
    cell.configureModel = _dataArray[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 20 * (kScreenWidth - 30) / 81 + 140 / 27;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SXWYDetailArticleViewController *artVC = [[SXWYDetailArticleViewController alloc] init];
    artVC.docidStr = ((SXWYRelativeModel *)self.dataArray[indexPath.row]).Id;
    [self.navigationController pushViewController:artVC animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
